package com.example.landlord.demo.utils.system;

import org.java_websocket.WebSocket;
import org.springframework.boot.json.JsonParser;
import org.springframework.boot.json.JsonParserFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

public class WebSocketMessage {
    Map<String, Object> data;   //存储解析后的命令信息
    WebSocket webSocket;        //存储由何人发送的信息

    int order;  //标识是何种命令
    String name;    //姓名
    String content; //内容
    String id;      //身份id
    int num;        //已读数量
    String picture;
    int game;       //标记游戏
    String roomId;  //房间唯一标识
    int operation;  //房间操作标识符
    int mode;       //操作模式
    int cmd;
    int[] idList;   //牌的id
    int score;      //叫分分值

    public WebSocketMessage(){}
    public WebSocketMessage(String json, WebSocket webSocket){
        JsonParser jsonParser = JsonParserFactory.getJsonParser();
        data = jsonParser.parseMap(json);
        order = (int) data.get("order");
        switch (order){
            case 0:
                recordUser();   //登记用户身份
                break;
            case 1:
//                operation();    //解析用户操作
                break;
            case 2:
                name = (String) data.get("roomName");
                break;
            case 3: //房间操作请求
                roomId=(String) data.get("roomId");
                game=(int) data.get("game");
                mode=(int) data.get("mode");
                switch (mode){
                    case 0:
                        operation=(int) data.get("operation");
                        break;
                    case 1:
                        roomId=(String) data.get("roomId");
                        break;
                    case 2:
                        cmd=(int) data.get("cmd");
                        gameOperation();
                        break;
                    case 3:
                        content=(String)data.get("content");
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                }
                break;
            case 4:                 //解析用户的请求哪个房间的列表
                game = (int) data.get("game");   //游戏类型
                break;
            case 5:
                break;
            case 6:         //查询是否处于游戏状态
//                game = (int) data.get("game");   //游戏类型
                break;
            case 7:
            case 9:
            case 8:
//                name="b";
                //                name="c";
                //                System.out.println("asd:"+roomId);
//                name="a";
                roomId = (String) data.get("roomId");
                break;
        }

        this.webSocket = webSocket;
    }

    void gameOperation(){
        switch (cmd){
            case 0:
                ArrayList<Integer> arrayList=(ArrayList<Integer>) data.get("idList");
                idList=new int[arrayList.size()];
                for(int i=0;i<arrayList.size();i++){
                    idList[i]=arrayList.get(i);
                }
                break;
            case 1:
                score=(int)data.get("score");
                break;
        }
    }

    void recordUser(){
        name =(String) data.get("name");
        id = (String) data.get("id");
        picture = (String) data.get("picture");
    }

    @Override
    public String toString() {
        return "WebSocketMessage{" +
                "data=" + data +
                ", webSocket=" + webSocket +
                ", order=" + order +
                ", name='" + name + '\'' +
                ", content='" + content + '\'' +
                ", id='" + id + '\'' +
                ", game=" + game +
                ", roomId='" + roomId + '\'' +
                ", operation=" + operation +
                ", mode=" + mode +
                ", cmd=" + cmd +
                ", idList=" + Arrays.toString(idList) +
                ", score=" + score +
                '}';
    }
}
